package com.wgcloud;

import com.google.common.io.Resources;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.nio.charset.Charset;
import java.util.Properties;

@SpringBootApplication
@ComponentScan(basePackages = "com.wgcloud")
@EnableCaching
@EnableScheduling
public class AgentApplication
{
	public static void main(String[] args) {
        SpringApplication.run(AgentApplication.class, args);
        initSigar();
    }

    public static String getOsType() {
        Properties props = System.getProperties();
        String os = props.getProperty("os.name", "").toLowerCase();
        if (os.contains("win")) {
            return "WIN";
        } else if (os.contains("mac") || os.contains("darwin")) {
            return "MAC";
        } else if (os.contains("mac") || os.contains("darwin")) {
            return "LINUX";
        } else {
            return "OTHER";
        }
    }

    private static void initSigar() {
        try {
            String os = getOsType();
            String filename = "libsigar-x86-linux.so";
            String pathSign = ":";

            switch (os) {
                case "WIN":
                    filename = "sigar-x86-winnt.dll";
                    pathSign = ";";
                    break;
                case "MAC":
                    filename = "libsigar-universal-macosx.dylib";
                    pathSign = ":";
                    break;
                case "LINUX":
                    filename = "libsigar-x86-linux.so";
                    pathSign = ":";
                    break;
                default:
                    break;
            }

            String file = Resources.getResource("sigar/" + filename).getFile();
            File classPath = new File(file).getParentFile();

            String path = System.getProperty("java.library.path");

            path += pathSign + classPath.getCanonicalPath();
            System.setProperty("java.library.path", path);

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    @Bean
    public RestTemplate restTemplate() {
        StringHttpMessageConverter m = new StringHttpMessageConverter(Charset.forName("UTF-8"));
        RestTemplate restTemplate = new RestTemplateBuilder().additionalMessageConverters(m).build();
        return restTemplate;
    }

    @Bean
    public TaskScheduler taskScheduler() {
        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        taskScheduler.setPoolSize(10);
        return taskScheduler;
    }

}
